package com.dddd.blog.blogpost.controller;


import com.dddd.blog.api.post.pojo.Post;
import com.dddd.blog.blogcommon.constant.ResultCode;
import com.dddd.blog.blogcommon.pojo.Result;
import com.dddd.blog.blogcommon.pojo.vo.PageQueryVO;
import com.dddd.blog.blogcommon.pojo.vo.PostVO;
import com.dddd.blog.blogcommon.utils.ResultUtils;
import com.dddd.blog.blogpost.service.IPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dddd
 * @since 2022-02-27
 */
@RestController
@RequestMapping("/post")
@Api(tags = {"PostController"},value = "/post",description = "博客管理接口")
public class PostController {

    /**
     * 文章业务层
     */
    @Autowired
    private IPostService postService;


    /** --------------------------------------------------*/


    /**
     * api生成示例1
     */
    @GetMapping("/list")
    @ApiOperation(value = "testtest", notes = "响应用户")
    public String test(@ApiParam String id){
        return "list";
    }

    /**
     * api生成示例2
     */
    @GetMapping("/test/{id}")
    @ApiOperation(value = "testtest", notes = "响应用户")
    public String test1(@ApiParam(name = "id") @PathVariable(name = "id")String id){
        return "list"+id;
    }


    /** ********************************************************************************************/



    @PostMapping("/list/p")
    @ApiOperation(value = "分页获取文章列表",notes = "需要当前页参数和页面显示条数")
    public Result<List<Post>> getPostListByPage(@ApiParam(name="currentPage",defaultValue = "1")
                                                @RequestParam("currentPage") Long currentPage,

                                                @ApiParam(name="pageSize", defaultValue = "10")
                                                @RequestParam("pageSize") Long pageSize)
    {
        if(currentPage<=0 || pageSize <=0){
            return ResultUtils.error(ResultCode.PARAMS_INVLIDA);
        }

        Map<String,Object> map = new HashedMap();
        //获取文章总数
        Integer total = postService.getPostCount();

        List<Post> postList = postService.getPostListByPage(currentPage, pageSize);

        return ResultUtils.success(postList);
    }


    @PostMapping("/page")
    @ApiOperation(value = "分页获取文章列表")
    public Result getPostByPage(@RequestBody PageQueryVO vo)
    {

        Map<String,Object> map = new HashedMap();
        //获取文章总数
        Integer total = postService.getPostCount();

        List<Post> postList = postService.getPostListByPage(vo.getCurrentPage(), vo.getPageSize());

        if(postList == null || postList.size()<=0){
            return ResultUtils.error(ResultCode.NOT_FOUND);
        }

        map.put("total",total);
        map.put("pages",total/vo.getPageSize());
        map.put("currentPage",vo.getCurrentPage());
        map.put("pageSize",vo.getPageSize());
        map.put("list",postList);

        return ResultUtils.success(map);
    }




    /**
     * @return
     */
    @GetMapping("/hot")
    @ApiOperation(value = "按阅读量、评论数降序分页查询")
    public Result getHomeHotPage(@RequestParam("currentPage") Long currentPage){

        List<Post> list = postService.getPostOrderByRead();

        Integer total = postService.getPostVisited();


        if(list==null || list.size()<=0){
            return ResultUtils.error(ResultCode.NOT_FOUND);
        }
        Map<String,Object> map = new HashedMap();

        map.put("total",total);
        map.put("pages",total/10);
        map.put("currentPage",currentPage);
        map.put("pageSize",10);
        map.put("list",list);

        return ResultUtils.success(map);
    }



    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取文章")
    public Result<Post> getPostById(@ApiParam("id") @PathVariable("id")Long id)
    {
        PostVO vo = postService.getPostById(id);
        return ResultUtils.success(vo);
    }


    @GetMapping("/count")
    @ApiOperation(value = "获取文章的数目")
    public Result getPostCount(){
        Integer count = postService.getPostCount();
        return ResultUtils.success(count);
    }


    /**
     * limit 15
     * @return
     */
    @GetMapping("/readRank")
    @ApiOperation(value = "按照阅读量查找热门文章")
    public Result getReadRank(){

        List<Map<String, Object>> readRank = postService.getReadRank();

        if(readRank!=null && readRank.size()>0){
            return ResultUtils.success(readRank);
        }
        return ResultUtils.error(ResultCode.NOT_FOUND);
    }


    /**
     * limit 15
     * @return
     */
    @GetMapping("/commentRank")
    @ApiOperation(value = "按照评论数查找热门文章")
    public Result getCommentRank(){

        List<Map<String, Object>> commentRank = postService.getCommentRank();

        if(commentRank != null && commentRank.size()>0){
            return ResultUtils.success(commentRank);
        }
        return ResultUtils.error(ResultCode.NOT_FOUND);
    }

    @GetMapping("/userHot/{name}")
    @ApiOperation(value = "根据name获取其热门列表")
    public Result getUserHot(@PathVariable("name")String name){

        List<Map<String, Object>> list = postService.getHotByAuthor(name);
        if(list!=null || list.size()>0){
            return ResultUtils.success(list);
        }
        return ResultUtils.error(ResultCode.NOT_FOUND);
    }



    @PostMapping("/add/map")
    @ApiOperation(value = "添加文章")
    public Result addOnePost(@RequestBody Map<String,Object> post){
        System.out.println("post添加文章Controlle");
        System.out.println(post);
        int id = postService.addPostByMap(post);
        return ResultUtils.success(id);
    }


    @PostMapping("/add/map-list")
    @ApiModelProperty(value = "添加文章Map列表")
    public Result addPostMapList(List<Map<String,Object>> list){

        if(list == null || list.isEmpty()){
            return ResultUtils.error(ResultCode.PARAMS_BLANK);
        }

        int i = postService.addPostByMapList(list);

        return ResultUtils.success(i);
    }
}
